//
//  MainViewController.swift
//  Swift微博
//
//  Created by 在下叶良辰 on 16/6/2.
//  Copyright © 2016年 谢浩源. All rights reserved.
//

import UIKit

// swift文件中, 对于不需要公开的属性和方法, 我们需要加一个private

// MARK - class里面只放生命周期方法, 构造方法, 属性
class MainViewController: UITabBarController {

    lazy var composeButton: UIButton = UIButton(title: "", image: "tabbar_compose_icon_add", backGroudImage: "tabbar_compose_button", target: self, action: "compose:", event: .TouchUpInside)
    
     override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        
        // 添加子控制器
        addChildViewControllers()
        // 设置跟添加中间 + 按钮
        addComposeButton()
        
//        tabBar.tintColor = UIColor.orangeColor()
        // 设置tabBar 的上面那条线
        settabBar()
    }
    
    /**
        tabBar的子view是在viewWillAppear才创建，所以为了让中间的按钮能点击 (放到最上层) 必须在展现后才把按钮设置添加进来
     */
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composeButton)
    }
    
    
}

// 分类 - 分类可以将相同逻辑的代码放到一个分类里面, 代码比较易读,层次相对清晰
extension MainViewController {
    
    /**
     *  添加子控制器
     
     - parameter controller 子控制器
     - parameter title title
     - parameter iconImageName tabBarItem的图片名字
     */
    private func addChildViewController(controller: UIViewController, title: String, iconImageName: String?) {
        let navigationHome = UINavigationController(rootViewController: controller)
        controller.title = title
        
        // 设置tabBar文字和 颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], forState: .Normal)
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        if let iconImageName = iconImageName {
            
            let selectedImageName = "\(iconImageName)_selected"
            //设置四个tabBar图片
            controller.tabBarItem.image = UIImage(named: iconImageName)?.imageWithRenderingMode(.AlwaysOriginal)
            controller.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.imageWithRenderingMode(.AlwaysOriginal)
        }
        
        addChildViewController(navigationHome)
    }
    
    
    /**
     *  给所有子控制器赋值
     */
    private func addChildViewControllers() {
        
        //添加主页
        let home = HomeViewController()
        addChildViewController(home, title: "首页", iconImageName: "tabbar_home")
        
        //消息页面
        let message = MessageViewController()
        addChildViewController(message, title:  "消息", iconImageName: "tabbar_message_center")
        
        //占位控制器
        let viewController = UIViewController()
        addChildViewController(viewController, title: "",iconImageName: nil)
        
        //发现页面
        let discover = DiscoverViewController()
        addChildViewController(discover, title: "发现", iconImageName: "tabbar_discover")
        
        //我 页面
        let profile = ProfileViewController()
        addChildViewController(profile, title: "我", iconImageName: "tabbar_profile")
    }
    
    private func addComposeButton () {
        self.tabBar.addSubview(composeButton)
        
        let tabBarItemWidth = tabBar.bounds.size.width/5
        let composeFrame = CGRectInset(tabBar.bounds, tabBarItemWidth*2, 0)
        
        composeButton.frame = composeFrame
    }
    
    // 设置tabbar 上面的一条线 (阴影跟背景)
    private func settabBar () {
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.shadowImage = UIImage.createAdotImage()
    }
    
    //unrecognized selector sent to instance 0x7ff58a804bb0':找不到指定的方法
    // (UIButton不在当前文件里面, 设置private会崩溃, 需要再加@objc)
    //selecotor是在运行时动态地向MainViewControll发送 compse 的消息。
    //@objc可以向MainViewControll发送 compse 的消息
    //用selector调用一个方法时，使用。
    @objc private func compose (button: UIButton) {
       print("发布")
        
    }
    
}
